package com.sky.mapper;


import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author Morpeko
 * @description 针对表【orders(订单表)】的数据库操作Mapper
 * @createDate 2024-08-17 20:49:57
 * @Entity com.sky.pojo.Orders
 */
@Mapper
public interface OrdersMapper {

    /**
     * 新增用户订单到订单表
     *
     * @param orders
     */
    void insert(Orders orders);

    /**
     * 根据订单号查询订单
     *
     * @param outTradeNo
     * @return
     */
    @Select("SELECT id, number, status, user_id, address_book_id, order_time, checkout_time, pay_method, pay_status, amount, remark, phone, address, user_name, consignee, cancel_reason, rejection_reason, cancel_time, estimated_delivery_time, delivery_status, delivery_time, pack_amount, tableware_number, tableware_status" +
            " FROM orders WHERE number = #{outTradeNo}")
    Orders getByNumber(String outTradeNo);

    /**
     * 动态修改订单信息
     *
     * @param orders
     */
    void update(Orders orders);

    /**
     * 根据用户ID和订单状态条件查询历史订单
     *
     * @param orders
     * @param <Orders>
     */
    //@Select("select * from orders where status = #{status} and user_id = #{userId}")
    List<Orders> pageQuery(Orders orders);

    /**
     * 根据订单ID查询订单明细信息
     *
     * @param orders1Id
     * @return
     */
    @Select("select id, name, image, order_id, dish_id, setmeal_id, dish_flavor, number, amount" +
            " from order_detail where order_id = #{orders1Id}")
    List<OrderDetail> getOrdersDetailByOrderId(Long orders1Id);

    /**
     * 根据订单id查询订单详情
     *
     * @param id
     * @return
     */
    @Select("select id, number, status, user_id, address_book_id, order_time, checkout_time, pay_method, pay_status, amount, remark, phone, address, user_name, consignee, cancel_reason, rejection_reason, cancel_time, estimated_delivery_time, delivery_status, delivery_time, pack_amount, tableware_number, tableware_status" +
            " from orders where id = #{id}")
    Orders getById(Long id);


    /**
     * 订单取消方法,修改订单状态为取消,修改取消时间,修改取消原因,修改支付状态为退款
     * 根据订单ID修改
     *
     * @param orders
     */

    void cancelOrder(Orders orders);

    /**
     * 根据订单号,用户手机号,订单状态,下单开始时间,下单结束时间进行条件分页查询
     *
     * @param ordersPageQueryDTO
     * @return
     */
    List<Orders> pageQueryByCondition(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 业务实现:根据订单状态统计各种状态的订单数量
     *
     * @param orderStatus
     * @return
     */
    @Select("select count(*) from orders where status = #{orderStatus}")
    Integer statistics(Integer orderStatus);

    /**
     * 根据订单状态和超时时间获取订单列表
     *
     * @param pendingPayment
     * @param timeoutTime
     * @return
     */
    @Select("select id, number, status, user_id, address_book_id, order_time, checkout_time, pay_method, pay_status, amount, remark, phone, address, user_name, consignee, cancel_reason, rejection_reason, cancel_time, estimated_delivery_time, delivery_status, delivery_time, pack_amount, tableware_number, tableware_status" +
            " from orders where status = #{pendingPayment} and order_time < #{timeoutTime}")
    List<Orders> getByStatusAndTimeout(Integer pendingPayment, LocalDateTime timeoutTime);

    /**
     * 根据Map集合条件查询订单数量
     * 根据当天时间范围查询订单数量
     *
     * @param map
     * @return
     */
    //@Select("select count(id) from orders where order_time >= #{begin} and order_time <= #{end} and status = #{status}")//查询当天订单数量
    Integer countByMap(Map map);

    /**
     * 营业额,当天时间范围内,已完成订单的总金额
     * @param map
     * @return
     */
    Double sumByMap(Map map);
}




